package com.gitee.deament.server.core.cli;

import cn.hutool.core.util.StrUtil;
import com.gitee.deament.server.core.annotation.CliHander;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;


/**
 * 命令处理类
 *
 * @author yanyu
 */

public class CliHandler {


    public void dealCli(ChannelHandlerContext channelHandlerContext, String command) {
        String commandKey = getCommand(command);
        if (command == null) {
           return ;
        }
        AbstractCli cli = CliContext.getCliList().get(commandKey);
        if (cli == null) {
            channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame("(error) ERR unknown command '"+command+"'"));
            return ;
        }
        cli.execute(channelHandlerContext, command);
    }

    public String getCommand(String command) {
        if (StrUtil.isBlank(command)) {
            return null;
        }
        //去除空格
        return command.trim().split(" ")[0].toUpperCase();


    }
}
